<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增权限资源管理')"/>
    <style>

        .mainBox {
            width: 90%;
            margin-top: 20px;
            position: absolute;
            top: 0px;
            left: 0px;
            bottom: 50px;
            overflow: auto;
        }

        .bottom {
            width: 100%;
            position: absolute;
            bottom: 0px;
            left: 0px;
            height: 50px;
            line-height: 50px;
            background-color: #F8F8F8;
            border-top: 1px solid #eee;
        }

        .button-container {
            position: absolute;
            right: 15px;
        }
        body{
            background-color: #fff;
        }
    </style>
</head>
<body>
<form class="layui-form">
    <div class="mainBox">
        <div class="main-container">
            <input type="hidden" name="enable" value="1">
            <div class="layui-form-item">
                <label class="layui-form-label">父级</label>
                <div class="layui-input-block">
                    <ul data-id="-1" id="selectParent" name="parentId"></ul>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">名称</label>
                <div class="layui-input-block">
                    <input type="text" name="name" placeholder="请输入资源名称"
                        class="layui-input" />
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">类型</label>
                <div class="layui-input-block">
                    <input lay-filter="type" name="type" title="目录" type="radio" value="0">
                    <input checked lay-filter="type" name="type" title="菜单" type="radio" value="1">
                    <input lay-filter="type" name="type" title="按钮" type="radio" value="2">
                </div>
            </div>
            <div class="layui-form-item" id="permissionItem">
                <label class="layui-form-label">权限标识</label>
                <div class="layui-input-block">
                    <input type="text" id="permission" name="permission" placeholder="请输入权限标识"
                        class="layui-input" />
                </div>
            </div>
            <div class="layui-form-item" id="hrefItem">
                <label class="layui-form-label">跳转地址</label>
                <div class="layui-input-block">
                    <input type="text" id="href" name="href" placeholder="请输入跳转地址"
                        class="layui-input" />
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">排序</label>
                <div class="layui-input-block">
                    <input type="text" name="sort" placeholder="请输入排序"
                        class="layui-input" />
                </div>
            </div>
            <div class="layui-form-item" id="openTypeItem">
                <label class="layui-form-label">打开方式</label>
                <div class="layui-input-block">
                    <select id="openType" name="openType">
                        <option value="1">框架内部</option>
                        <option value="0">新标签页</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">图标</label>
                <div class="layui-input-block">
                    <input class="layui-input" id="icon" value="layui-icon-home" lay-filter="icon" name="icon" type="text">
                </div>
            </div>

        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="layui-btn layui-btn-primary layui-btn-sm" lay-submit="" lay-filter="permission-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="layui-btn layui-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
</body>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">

    layui.use(['form', 'jquery', 'iconPickerFa', 'dtree', 'dictionary'], function () {
        let form = layui.form;
        let $ = layui.jquery;
        let iconPickerFa = layui.iconPickerFa;
        let dtree = layui.dtree;

        let prefix = pName + "/system/permission/"

        // String parentId, String permission, String href
        let parentId = common.getUrlParam("parentId");
        let permission = common.getUrlParam("permission");
        let href = common.getUrlParam("href");
        if(!!permission){
            let end = permission.lastIndexOf(':')
            if(end == 0){
                $('#permission').val(permission + ":[??]")
            } else {
                $('#permission').val(permission.substring(0,permission.lastIndexOf(':')) + ":[??]")
            }
        }
        if(!!href){
            let end = permission.lastIndexOf('/')
            if(end == 0){
                $('#href').val(href + "/[??]")
            } else {
                $('#href').val(href.substring(0,href.lastIndexOf('/')) + "/[??]")
            }
        }
        iconPickerFa.render({
            // 选择器，推荐使用input
            elem: '#icon',
            // fa 图标接口
            url: pName + "/layuimini/lib/font-awesome-4.7.0/less/variables.less",
            // 是否开启搜索：true/false，默认true
            search: true,
            // 是否开启分页：true/false，默认true
            page: true,
            // 每页显示数量，默认12
            limit: 12,
            // 点击回调
            click: function (data) {
                console.log(data);
            },
            // 渲染成功后的回调
            success: function (d) {
                console.log(d);
            }
        });


        dtree.renderSelect({
            elem: "#selectParent",
            url: pName + "/system/permission/power/selectParent",
            method: 'get',
            selectInputName: {nodeId: "parentId", context: "parentName"},
            skin: "layui",
            dataFormat: "list",
            response: {treeId: "id", parentId: "parentId", title: "name"},  //修改response中返回数据的定义
            selectInitVal: parentId
        });

        form.on("radio(type)", function () {
            if (this.value == '0') {
                $("#hrefItem").hide();
                $("#permissionItem").hide();
                $("#openTypeItem").hide();
                $("#href").val("");
                $("#permission").val("");
                $("#openType").val("");
            } else if (this.value == '1') {
                $("#hrefItem").show();
                $("#permissionItem").show();
                $("#openTypeItem").show();
            } else if (this.value == '2') {
                $("#hrefItem").hide();
                $("#openTypeItem").hide();
                $("#permissionItem").show();
                $("#href").val("");
                $("#openType").val("");
            }
        });

        form.on('submit(permission-save)', function (data) {
            if($('#permission').val().indexOf("?") > -1){
                layer.msg('权限标识不合法')
                return
            }
            if($('#href').val().indexOf("?") > -1){
                layer.msg('跳转地址不合法')
                return
            }

            let loading = layer.load();
            for (var key in data.field) {
                var type = $(data.form).find("input[name='" + key + "']").attr("type");
                if (type == "checkbox") {
                    var value = "";
                    $(data.form).find("input[name='" + key + "']:checked").each(function () {
                        value += $(this).val() + ",";
                    })
                    if (value != "") {
                        value = value.substr(0, value.length - 1);
                        data.field[key] = value;
                    }

                }
            }
            $.ajax({
                url: prefix + 'save',
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (result) {
                    layer.close(loading);
                    if (result.code == 1) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));
                            parent.layui.table.reload("permission-table");
                            parent.layui.treetable.reload("permission-table");
                        });
                    } else {
                            layer.alert(result.msg, {icon: 2});
                        }
                }
            })
            return false;
        });
    });
</script>
</html>